﻿//------------------------------------------------------------------------------
// 创建标识: Copyright (C) 2010 Socansoft.com 版权所有
// 创建描述: SocanCode代码生成器 V4.3.3.6 自动创建于 2010/4/20 11:21:24
//
// 功能描述: 
//
// 修改标识: 
// 修改描述: 
//------------------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Text;
using System.Collections;
using System.Collections.Generic;

namespace OPMS_IBS.SqlServerDAL
{
	/// <summary>
	/// 数据访问类 TimeTable
	/// </summary>
	public class TimeTable : DALHelper
	{
		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(OPMS_IBS.Model.TimeTable model)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("INSERT INTO TimeTable(");
            strSql.Append("OwnLineIBDMID,TimeTableName,BeginDate,EndDate,StartStationName,EndStationName,TokenAmount,TurnAmount,ShowRowsAmount,ShowColsAmount,Level,Type,Valid,CreateDate)");
			strSql.Append(" VALUES (");
            strSql.Append("@in_OwnLineIBDMID,@in_TimeTableName,@in_BeginDate,@in_EndDate,@in_StartStationName,@in_EndStationName,@in_TokenAmount,@in_TurnAmount,@in_ShowRowsAmount,@in_ShowColsAmount,@in_Level,@in_Type,@in_Valid,@in_CreateDate)");
			DbParameter[] cmdParms = {
				OPMS_IBSHelper.CreateInDbParameter("@in_OwnLineIBDMID", DbType.AnsiString, model.OwnLineIBDMID),
				OPMS_IBSHelper.CreateInDbParameter("@in_TimeTableName", DbType.AnsiString, model.TimeTableName),
				OPMS_IBSHelper.CreateInDbParameter("@in_BeginDate", DbType.String, model.BeginDate),
				OPMS_IBSHelper.CreateInDbParameter("@in_EndDate", DbType.String, model.EndDate),
				OPMS_IBSHelper.CreateInDbParameter("@in_StartStationName", DbType.AnsiString, model.StartStationName),
				OPMS_IBSHelper.CreateInDbParameter("@in_EndStationName", DbType.AnsiString, model.EndStationName),
				OPMS_IBSHelper.CreateInDbParameter("@in_TokenAmount", DbType.Int32, model.TokenAmount),
				OPMS_IBSHelper.CreateInDbParameter("@in_TurnAmount", DbType.Int32, model.TurnAmount),
				OPMS_IBSHelper.CreateInDbParameter("@in_ShowRowsAmount", DbType.Int32, model.ShowRowsAmount),
				OPMS_IBSHelper.CreateInDbParameter("@in_ShowColsAmount", DbType.Int32, model.ShowColsAmount),
				OPMS_IBSHelper.CreateInDbParameter("@in_Level", DbType.Int32, model.Level),
				OPMS_IBSHelper.CreateInDbParameter("@in_Type", DbType.AnsiString, model.Type),
				OPMS_IBSHelper.CreateInDbParameter("@in_Valid", DbType.Boolean, model.Valid),
				OPMS_IBSHelper.CreateInDbParameter("@in_CreateDate", DbType.String, model.CreateDate)};

			return OPMS_IBSHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), cmdParms);
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public int Update(OPMS_IBS.Model.TimeTable model)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("UPDATE TimeTable SET ");
			strSql.Append("OwnLineIBDMID=@in_OwnLineIBDMID,");
			strSql.Append("TimeTableName=@in_TimeTableName,");
			strSql.Append("BeginDate=@in_BeginDate,");
			strSql.Append("EndDate=@in_EndDate,");
			strSql.Append("StartStationName=@in_StartStationName,");
			strSql.Append("EndStationName=@in_EndStationName,");
			strSql.Append("TokenAmount=@in_TokenAmount,");
			strSql.Append("TurnAmount=@in_TurnAmount,");
			strSql.Append("ShowRowsAmount=@in_ShowRowsAmount,");
			strSql.Append("ShowColsAmount=@in_ShowColsAmount,");
			strSql.Append("Level=@in_Level,");
			strSql.Append("Type=@in_Type,");
			strSql.Append("Valid=@in_Valid,");
			strSql.Append("CreateDate=@in_CreateDate");
			strSql.Append(" WHERE ID=@in_ID");
			DbParameter[] cmdParms = {
				OPMS_IBSHelper.CreateInDbParameter("@in_OwnLineIBDMID", DbType.AnsiString, model.OwnLineIBDMID),
				OPMS_IBSHelper.CreateInDbParameter("@in_TimeTableName", DbType.AnsiString, model.TimeTableName),
				OPMS_IBSHelper.CreateInDbParameter("@in_BeginDate", DbType.String, model.BeginDate),
				OPMS_IBSHelper.CreateInDbParameter("@in_EndDate", DbType.String, model.EndDate),
				OPMS_IBSHelper.CreateInDbParameter("@in_StartStationName", DbType.AnsiString, model.StartStationName),
				OPMS_IBSHelper.CreateInDbParameter("@in_EndStationName", DbType.AnsiString, model.EndStationName),
				OPMS_IBSHelper.CreateInDbParameter("@in_TokenAmount", DbType.Int32, model.TokenAmount),
				OPMS_IBSHelper.CreateInDbParameter("@in_TurnAmount", DbType.Int32, model.TurnAmount),
				OPMS_IBSHelper.CreateInDbParameter("@in_ShowRowsAmount", DbType.Int32, model.ShowRowsAmount),
				OPMS_IBSHelper.CreateInDbParameter("@in_ShowColsAmount", DbType.Int32, model.ShowColsAmount),
				OPMS_IBSHelper.CreateInDbParameter("@in_Level", DbType.Int32, model.Level),
				OPMS_IBSHelper.CreateInDbParameter("@in_Type", DbType.AnsiString, model.Type),
				OPMS_IBSHelper.CreateInDbParameter("@in_Valid", DbType.Boolean, model.Valid),
				OPMS_IBSHelper.CreateInDbParameter("@in_CreateDate", DbType.String, model.CreateDate),
				OPMS_IBSHelper.CreateInDbParameter("@in_ID", DbType.Guid, model.ID)};
			return OPMS_IBSHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), cmdParms);
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public int Delete(Guid ID)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("DELETE FROM TimeTable ");
			strSql.Append(" WHERE ID=@in_ID");
			DbParameter[] cmdParms = {
				OPMS_IBSHelper.CreateInDbParameter("@in_ID", DbType.Guid, ID)};

			return OPMS_IBSHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), cmdParms);
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(Guid ID)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("SELECT COUNT(1) FROM TimeTable");
			strSql.Append(" WHERE ID=@in_ID");
			DbParameter[] cmdParms = {
				OPMS_IBSHelper.CreateInDbParameter("@in_ID", DbType.Guid, ID)};

			object obj = OPMS_IBSHelper.ExecuteScalar(CommandType.Text, strSql.ToString(), cmdParms);
			return OPMS_IBSHelper.GetInt(obj) > 0;
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public OPMS_IBS.Model.TimeTable GetModel(Guid ID)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("SELECT * FROM TimeTable ");
			strSql.Append(" WHERE ID=@in_ID");
			DbParameter[] cmdParms = {
				OPMS_IBSHelper.CreateInDbParameter("@in_ID", DbType.Guid, ID)};

			OPMS_IBS.Model.TimeTable model = null;
			using (DbDataReader dr = OPMS_IBSHelper.ExecuteReader(CommandType.Text, strSql.ToString(), cmdParms))
			{
				while (dr.Read())
				{
					model = GetModel(dr);
				}
				return model;
			}
		}

		/// <summary>
		/// 获取泛型数据列表
		/// </summary>
		public List<OPMS_IBS.Model.TimeTable> GetList()
		{
			StringBuilder strSql = new StringBuilder("SELECT * FROM TimeTable");
			using (DbDataReader dr = OPMS_IBSHelper.ExecuteReader(CommandType.Text, strSql.ToString(), null))
			{
				List<OPMS_IBS.Model.TimeTable> lst = GetList(dr);
				return lst;
			}
		}

		/// <summary>
		/// 得到数据条数
		/// </summary>
		public int GetCount(string condition)
		{
			return OPMS_IBSHelper.GetCount("TimeTable", condition);
		}

		/// <summary>
		/// 分页获取泛型数据列表
		/// </summary>
		public List<OPMS_IBS.Model.TimeTable> GetPageList(int pageSize, int pageIndex, string fldSort, bool fldDir, string condition)
		{
			using (DbDataReader dr = OPMS_IBSHelper.GetPageList("TimeTable", pageSize, pageIndex, fldSort, fldDir, condition))
			{
				List<OPMS_IBS.Model.TimeTable> lst = GetList(dr);
				return lst;
			}
		}

		#region -------- 私有方法，通常情况下无需修改 --------

		/// <summary>
		/// 由一行数据得到一个实体
		/// </summary>
		private OPMS_IBS.Model.TimeTable GetModel(DbDataReader dr)
		{
			OPMS_IBS.Model.TimeTable model = new OPMS_IBS.Model.TimeTable();
			model.ID = OPMS_IBSHelper.GetGuid(dr["ID"]);
			model.OwnLineIBDMID = OPMS_IBSHelper.GetString(dr["OwnLineIBDMID"]);
			model.TimeTableName = OPMS_IBSHelper.GetString(dr["TimeTableName"]);
			model.BeginDate = OPMS_IBSHelper.GetDateTime(dr["BeginDate"]);
			model.EndDate = OPMS_IBSHelper.GetDateTime(dr["EndDate"]);
			model.StartStationName = OPMS_IBSHelper.GetString(dr["StartStationName"]);
			model.EndStationName = OPMS_IBSHelper.GetString(dr["EndStationName"]);
			model.TokenAmount = OPMS_IBSHelper.GetInt(dr["TokenAmount"]);
			model.TurnAmount = OPMS_IBSHelper.GetInt(dr["TurnAmount"]);
			model.ShowRowsAmount = OPMS_IBSHelper.GetInt(dr["ShowRowsAmount"]);
			model.ShowColsAmount = OPMS_IBSHelper.GetInt(dr["ShowColsAmount"]);
			model.Level = OPMS_IBSHelper.GetInt(dr["Level"]);
			model.Type = OPMS_IBSHelper.GetString(dr["Type"]);
			model.Valid = OPMS_IBSHelper.GetBool(dr["Valid"]);
			model.CreateDate = OPMS_IBSHelper.GetDateTime(dr["CreateDate"]);
			return model;
		}

		/// <summary>
		/// 由DbDataReader得到泛型数据列表
		/// </summary>
		private List<OPMS_IBS.Model.TimeTable> GetList(DbDataReader dr)
		{
			List<OPMS_IBS.Model.TimeTable> lst = new List<OPMS_IBS.Model.TimeTable>();
			while (dr.Read())
			{
				lst.Add(GetModel(dr));
			}
			return lst;
		}

		#endregion

        #region User Code

        /// <summary>
        /// 得到一个在用时刻表对象实体
        /// </summary>
        public Guid GetValidModel(int id, string type)
        {
            Guid result = Guid.Empty;
            string strSql =
                string.Format(
                    @"SELECT [ID]
                      FROM [IBS_Test].[dbo].[TimeTable]
                      WHERE [OwnLineIBDMID] = @ownLineIBDMID
                      AND [Type] = @type");
            DbParameter[] cmdParms = {
                                         OPMS_IBSHelper.CreateInDbParameter("@ownLineIBDMID", DbType.Int32, id),
                                         OPMS_IBSHelper.CreateInDbParameter("@type", DbType.String, type)
                                     };

            object obj = OPMS_IBSHelper.ExecuteScalar(CommandType.Text, strSql, cmdParms);

            if (obj != null)
            {
                result = OPMS_IBSHelper.GetGuid(obj);
            }

            return result;
        }

        /// <summary>
        /// 得到一个在用时刻表对象实体
        /// </summary>
        public List<OPMS_IBS.Model.TimeTable> GetValidModels(int id)
        {
            Guid result = Guid.Empty;
            string strSql =
                string.Format(
                    @"SELECT *
                      FROM [IBS_Test].[dbo].[TimeTable]
                      WHERE [OwnLineIBDMID] = @ownLineIBDMID 
                      ");
            DbParameter[] cmdParms = {
                                         OPMS_IBSHelper.CreateInDbParameter("@ownLineIBDMID", DbType.Int32, id),
                                         //OPMS_IBSHelper.CreateInDbParameter("@type", DbType.String, type)
                                     };

            using (DbDataReader dr = OPMS_IBSHelper.ExecuteReader(CommandType.Text, strSql.ToString(), cmdParms))
            {
                List<OPMS_IBS.Model.TimeTable> lst = GetList(dr);
                return lst;
            }
        }

        #endregion
	}
}
